package com.sunsy.authserver.service.impl;

import com.sunsy.authserver.beans.R;
import com.sunsy.authserver.service.CaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("captchaService")
public class CaptchaServiceImpl implements CaptchaService {

    private RedisOperator redisOperator;

    @Autowired
    private void setRedisOperator(RedisOperator redisOperator){
        this.redisOperator=redisOperator;
    }

    public String sendMobileCode(String mobile) {
        String code=String.valueOf(Math.random()*100000+100000).substring(0,6);
        //10分钟有效期
        redisOperator.set(mobile,code,60*10);
        return code;
    }

    public String sendEmailCode(String email) {
        String code=String.valueOf(Math.random()*100000+100000).substring(0,6);
        //10分钟有效期
        redisOperator.set(email,code,60*10);
        return code;
    }

    @Override
    public String sendCode(String type, String value) {
        switch (type) {
            case "mobile":
                return sendMobileCode(value);
            case "email":
                return sendEmailCode(value);
        }
        return null;
    }
}
